There is a problem with dplyr and plotly. When loaded tofether both have functions of same name so must point to the library you reall need, i.e. dplyr::select()
#library(reticulate)
library(tidyverse)
library(plotly)
library(janitor)
library(readxl)
library(xlsx)
Remove the players that are out or on the injured reserve
player_projection <- read_csv("DFF_NHL_cheatsheet.csv", col_names = TRUE) %>%
dplyr::filter(!injury_status %in% c("O", "IR")) %>% # remove the out and injured reserve players
dplyr::mutate(salary_factor = salary*.001, range_factor = (L5_ppg_max-L5_ppg_floor)/2, total_score = salary_factor + range_factor + ppg_projection + value_projection, pick = "x" )
slice takes the top 3 grouped records
later I ungroup and combine field names.
Then I filter out points above 60 to create a new object for graphing
team__reg_line <- player_projection %>%
clean_names() %>%
mutate_if(is.numeric, ~replace_na(., 0)) %>%
filter(!position == "G") %>%
filter(!reg_line == 0) %>%
select(team, reg_line, total_score, salary) %>%
arrange(team, reg_line, desc(total_score)) %>%
group_by(team, reg_line) %>%
dplyr::slice(1:3) %>%
dplyr::summarise(total_pts = sum(total_score), salary = sum(salary)) %>%
dplyr::ungroup() %>%
unite(team_line, team, reg_line, sep = "_", remove = TRUE)
## `summarise()` has grouped output by 'team'. You can override using the
## `.groups` argument.
team_pts_max_20 <- team__reg_line %>%
head(20)
this is called a lollypop chart
# install.packages("ggplot2")
library(ggplot2)
ggplot(team_pts_max_20, aes(x = team_line, y = salary)) +
geom_segment(aes(x = reorder(team_line, total_pts), xend = team_line, y = 5000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = round(total_pts)), color = "white", size = 3)
## Use plotly with ggplot2
p <- ggplot(team_pts_max_20, aes(x = team_line, y = salary)) +
geom_segment(aes(x = reorder(team_line, total_pts), xend = team_line, y = 5000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = round(total_pts)), color = "white", size = 3)
ggplotly(p)
Review choices for goalie bases on DK Spreadsheet
Filter for Goalies only
Reduce data elements
sort descending value
create a variance/spread column replacing l5_
create new field names for connecting to other databases
goalies <- player_projection %>%
clean_names() %>%
mutate_if(is.numeric, ~replace_na(., 0)) %>%
filter(position == "G") %>%
select(1:3, 5:6, 8, 10:16, 18:20, 23:26) %>%
arrange(desc(total_score), desc(salary)) %>%
unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
unite(name_team, last_name, team, sep = "_", remove = FALSE)
goalie_select_10 <- goalies %>%
head(10)
# install.packages("ggplot2")
library(ggplot2)
ggplot(goalie_select_10, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 5000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = total_score), color = "white", size = 3)
p <- ggplot(goalie_select_10, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 5000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = total_score), color = "white", size = 3)
ggplotly(p)
centers <- player_projection %>%
clean_names() %>%
mutate_if(is.numeric, ~replace_na(., 0)) %>%
filter(position == "C") %>%
select(1:3, 5:6, 8, 10:16, 18:20, 23:26) %>%
arrange(desc(total_score), desc(salary)) %>%
unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
unite(name_team, last_name, team, sep = "_", remove = FALSE)
center_select_15 <- centers %>%
head(15)
# install.packages("ggplot2")
library(ggplot2)
ggplot(center_select_15, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = total_score), color = "white", size = 3)
p <- ggplot(center_select_15, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = total_score), color = "white", size = 3)
ggplotly(p)
wings <- player_projection %>%
clean_names() %>%
mutate_if(is.numeric, ~replace_na(., 0)) %>%
filter(position == "W") %>%
select(1:3, 5:6, 8, 10:16, 18:20, 23:26) %>%
arrange(desc(total_score), desc(salary)) %>%
unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
unite(name_team, last_name, team, sep = "_", remove = FALSE)
wing_select_15 <- wings %>%
head(15)
# install.packages("ggplot2")
library(ggplot2)
ggplot(wing_select_15, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = total_score), color = "white", size = 3)
## Create the wing Selection chart in plotly
p <- ggplot(wing_select_15, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = total_score), color = "white", size = 3)
ggplotly(p)
defense <- player_projection %>%
clean_names() %>%
mutate_if(is.numeric, ~replace_na(., 0)) %>%
filter(position == "D") %>%
select(1:3, 5:6, 8, 10:16, 18:20, 23:26) %>%
arrange(desc(total_score), desc(salary)) %>%
unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
unite(name_team, last_name, team, sep = "_", remove = FALSE)
defense_select_15 <- defense %>%
head(15)
# install.packages("ggplot2")
library(ggplot2)
ggplot(defense_select_15, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = total_score), color = "white", size = 3)
## Create the defensemen Selection chart in plotly
p <- ggplot(defense_select_15, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = total_score), color = "white", size = 3)
ggplotly(p)
combo_player <- do.call("rbind", list(goalies, centers, wings, defense)) %>%
relocate(pick, .before = pp_line)
write_csv(combo_player, "combo_player.csv")
# Write the dataset to excel (OLD CODE)
#write.xlsx(combo_player, file = "combo_player.xlsx", sheetName = "combo_player", append = FALSE)
# Write the dataset to excel
combo_player <- as.data.frame(combo_player)
write.xlsx(combo_player,
file = paste("extra/My_Picks/combo_player_", Sys.Date(),
".xlsx", sep = ""),
sheetName = "combo_player", row.names = FALSE , append = FALSE)